package com.floreantpos.model;

import java.util.List;

import com.floreantpos.model.base.BaseTicketItemModifier;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

public class TicketItemModifier extends BaseTicketItemModifier {

  private static final long serialVersionUID = 1L;

  public final static int MODIFIER_NOT_INITIALIZED = 0;
  public final static int NORMAL_MODIFIER = 1;
  public final static int NO_MODIFIER = 2;
  public final static int EXTRA_MODIFIER = 3;

  /*[CONSTRUCTOR MARKER BEGIN]*/
  public TicketItemModifier() {
    super();
  }

  /**
   * Constructor for primary key
   */
  public TicketItemModifier(java.lang.Integer id) {
    super(id);
  }

  /*[CONSTRUCTOR MARKER END]*/
  private int tableRowNum;

  public int getTableRowNum() {
    return tableRowNum;
  }

  public void setTableRowNum(int tableRowNum) {
    this.tableRowNum = tableRowNum;
  }

  @Override
  public String toString() {
    return getName();
  }

  private int getPreviousItemsCount() {
    TicketItemModifierGroup ticketItemModifierGroup = getParent();
    List<TicketItemModifier> ticketItemModifiers = ticketItemModifierGroup.getTicketItemModifiers();

    int count = 0;
    for (TicketItemModifier modifier : ticketItemModifiers) {
      if (modifier == this) {
        return count;
      }
      if (modifier.getModifierType() != TicketItemModifier.NO_MODIFIER) {
        count += modifier.getItemCount();
      }
    }
    return count;
  }

  double calculateTotal() {
    double total = 0;

    TicketItemModifierGroup ticketItemModifierGroup = getParent();
    if (ticketItemModifierGroup == null) {
      setTotalAmount(total);
      return total;
    }

    int maxItemCount = ticketItemModifierGroup.getMaxQuantity();

    int normalItemCount = 0;
    int extraItemCount = 0;

    if (getItemCount() <= maxItemCount) {
      normalItemCount = getItemCount();
      extraItemCount = 0;
    } else {
      normalItemCount = maxItemCount;
      extraItemCount = getItemCount() - maxItemCount;
    }

    total = normalItemCount * getUnitPrice();
    total += extraItemCount * getExtraUnitPrice();

    setTotalAmount(total);
    return total;
  }
  
  public String getItemCountAsString() {
      DecimalFormat decimalFormat = new DecimalFormat("#,##0", new DecimalFormatSymbols(new Locale("pt", "BR")));
      String itemCountAsString = decimalFormat.format(Double.valueOf(getItemCount())).replace(".", ""); 
      String formattedItemCountAsString = "0000".substring(itemCountAsString.length()) + itemCountAsString;
      return formattedItemCountAsString;
  }
  
  public String getUnitPriceAsString() {
      Integer itemCount = 1;
      if (getItemCount() > 0) {
        itemCount = getItemCount();
      }
      DecimalFormat decimalFormat = new DecimalFormat("#,##0.00", new DecimalFormatSymbols(new Locale("pt", "BR")));
      String unitPriceAsString = decimalFormat.format(Double.valueOf(getTotalAmount()/itemCount)).replace(".", ""); 
      unitPriceAsString = unitPriceAsString.replace(",", "");
      String formattedUnitPriceAsString = "00000000".substring(unitPriceAsString.length()) + unitPriceAsString;
      return formattedUnitPriceAsString;
  }  
}
